#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 时间     : 2021-09-02 12:27
# 作者     : DL (584054432@qq.com)
# 网址     : https://gitee.com/dingliangPython/
# 软件版本 : Python3.7.3
# 功能     ：
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base

base = declarative_base()


# app表
class config_table(base):
    __tablename__ = "AppConfig"
    AppId = Column('AppId', Integer(), comment = '主键', server_default = '0', primary_key = True)
    AppName = Column('AppName', String(100), comment = '程序名称', server_default = 'APP')
    StartSdk = Column('StartSdk', Integer(), comment = '启用sdk')
    StartS7 = Column('StartS7', Integer(), comment = '启用s7')
    EnablePassword = Column('EnablePassword', Integer(), comment = '启用密码')


# 数据库配置表
class connect_table(base):
    __tablename__ = "ConnectConfig",
    id = Column('id', Integer(), comment = '主键', autoincrement = True, primary_key = True)
    name = Column('name', String(50), comment = '名称')
    type = Column('type', String(50), comment = '类型')
    mode = Column('mode', Integer, comment = '模式')
    host = Column('host', String(20), comment = '主机')
    port = Column('port', Integer, comment = '端口')
    username = Column('username', String(50), comment = '用户')
    password = Column('password', String(50), comment = '密码')
    database = Column('database', String(50), comment = '库实例')
    dsn = Column('dsn', String(50), comment = 'ODBC连接名称')
    mincached = Column('mincached', Integer, comment = '池初始连接数')
    maxconnections = Column('maxconnections', Integer, comment = '池最大连接数')
    databasepath = Column('databasepath', String(100), comment = '数据库路径')
    format = Column('format', String(10), comment = '格式')
    left = Column('left', Integer, comment = '在左边')
    right = Column('right', Integer, comment = '在右边')


# 字段表
class field_table(base):
    __tablename__ = "FieldConfig"
    Id = Column('Id', Integer(), comment = '序号',nullable=True)
    Description = Column('Description', String(50), comment = '注释')
    TableName = Column('TableName', String(50), comment = '源表名称')
    TargetTable = Column('TargetTable', String(50), comment = '目的表名称')
    FieldName = Column('FieldName', String(500), comment = '源字段')
    TargetField = Column('TargetField', String(50), comment = '目的字段')
    DefaultValue = Column('DefaultValue', String(50), comment = '默认值')
    Mode = Column('Mode', Integer, comment = '模式')
    SerialNumber = Column('SerialNumber', String(10), comment = '序号')


# 日志配置表
class log_table(base):
    __tablename__ = "LogConfig"
    LogLevel = Column('LogLevel', Integer, comment = '日志级别')
    LogSave = Column('LogSave', Integer, comment = '是否存储')
    LogSavePath = Column('LogSavePath', String(200), comment = '存储路径')
    LogSaveCycle = Column('LogSaveCycle', Integer, comment = '存储周期')
    LogSaveUnit = Column('LogSaveUnit', String(10), comment = '周期单位')
    LogSaveNumber = Column('LogSaveNumber', Integer, comment = '日志数量')
    LogSaveSize = Column('LogSaveSize', Integer, comment = '日志大小')


# 任务配置表
class scheduler_table(base):
    __tablename__ = "SchedulerConfig"

    ID = Column('ID', Integer, comment = '主键')
    Description = Column('Description', String(200), comment = '注释')
    Name = Column('Name', String(200), comment = '任务名称')
    Cycle = Column('Cycle', Integer, comment = '调度周期')
    AutoStart = Column('AutoStart', Integer, comment = '自启')
    SourceConnectName = Column('SourceConnectName', String(50), comment = '源连接名称')
    TagConnectName = Column('TagConnectName', String(50), comment = '目的连接')
    DateField = Column('DateField', String(50), comment = '源日期字段')
    SerialField = Column('SerialField', String(50), comment = '流水主键')
    MarkField = Column('MarkField', String(50), comment = '标记字段')
    MarkFieldInitValue = Column('MarkFieldInitValue', String(50), comment = '标记值')
    MarkFieldEndValue = Column('MarkFieldEndValue', String(50), comment = '标记更新值')
    Other = Column('Other', String(200), comment = '其它条件')
    Mode = Column('Mode', Integer, comment = '模式')
    SerialNumber = Column('SerialNumber', Integer, comment = '序号')


# 串口配置表
class serial_table(base):
    __tablename__ = "SerialConfig"
    port = Column('port', String(50), comment = '串口')
    baudrate = Column('baudrate', Integer, comment = '速率')
    bytesize = Column('bytesize', Integer, comment = '数据位')
    parity = Column('parity', String(10), comment = '校验位')
    stopbits = Column('stopbits', FLOAT(5), comment = '停止位')
    timeout = Column('timeout', FLOAT(10), comment = '超时')


# s7配置表
class s7_table(base):
    __tablename = "s7Config"
    name = Column('name', String(50), comment = '串口')
    host = Column('host', String(20), comment = '速率')
    port = Column('port', Integer, comment = '端口')
    rack = Column('rack', Integer, comment = '板卡')
    slot = Column('slot', Integer, comment = '插槽')
    tsap = Column('tsap', String(10), comment = '通用电缆')


# modbus配置表
class modbus_table(base):
    __tablename__ = "ModbusConfig"
    name = Column('name', String(50), comment = '名称')
    mode = Column('mode', String(50), comment = '模式')
    host = Column('host', String(20), comment = '主机')
    port = Column('port', Integer, comment = '端口')
    com = Column('com', String(50), comment = '串口')
    baudrate = Column('baudrate', Integer, comment = '速率')
    bytesize = Column('bytesize', Integer, comment = '数据位')
    parity = Column('parity', String(10), comment = '校验位')
    stopbits = Column('stopbits', FLOAT(5), comment = '停止位')
    timeout = Column('timeout', FLOAT(10), comment = '超时')


# 标记配置表
class flag_table(base):
    __tablename__ = "LastMark"
    Mark = Column('Mark', String(50), comment = '最后标记')
    UpdateTime = Column('UpdateTime', String(50), comment = '更新时间')
    TaskName = Column('TaskName', String(50), comment = '任务名称')


# 初始化本地表
def init_config(local):
    base.metadata.create_all(local.engine)
    with local.get_session() as session:

        session.autocommit = True
        data = session.query(config_table).all()
        # 初始程序表
        if not data:
            ins = config_table.insert()
            val = ins.values(AppId = 1)
            session.execute(val)
        # 日志表
        data = session.query(log_table).all()
        if not data:
            ins = log_table.insert()
            val = ins.values([10, 1, './log', 1, 'D', 10, 5])
            session.execute(val)